from datetime import datetime

from pydantic import BaseModel, EmailStr, Field, validator
from typing import Optional

from common import logger


class UserBase(BaseModel):
    username: Optional[str] = Field(None, description="用户名")  # Field 函数除了设置默认值为 None 外，还允许你提供额外的参数description
    email: Optional[EmailStr] = Field(None, description="邮箱")
    phone: Optional[str] = Field(None, description="手机号")
    nickname: Optional[str] = Field(None, description="昵称")
    avatar: Optional[str] = Field(None, description="头像")


class UserCreate(UserBase):
    password: str = Field(..., description="密码")

    # todo 创建用户时用户名、邮箱、手机号3个字段必须至少有一个不为空


class UserUpdate(BaseModel):
    username: Optional[str] = Field(None, description="用户名")
    nickname: Optional[str] = Field(None, description="昵称")


class UserSchema(UserBase):
    id: int = Field(..., description="ID")  # Field(...表示必填
    create_time: datetime = Field(..., description="创建时间")
    update_time: datetime = Field(..., description="更新时间")
    is_delete: int = Field(0, description="逻辑删除:0=未删除,1=删除")

    class Config:
        from_attributes = True
